package menu.sys

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import org.apache.commons.io.FileUtils
import org.junit.Test
import java.io.File

/**
 * @author futao
 * Created on 2017/12/8 - 10:12.
 */
class MenuTest {

    @Test
    fun testCase001() {
        val menuObj = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\menu.json"))).asJsonObject
        val oldRoleObj = JsonParser().parse(FileUtils.readFileToString(File("C:\\Users\\11851\\Desktop\\role.json"))).asJsonObject
        val newRoleObj = JsonObject()
        menuObj.entrySet().forEach { (menu_nav_k, menu_nav_v) ->
            val new_role_nav_v = JsonObject()
            newRoleObj.add(menu_nav_k, new_role_nav_v)


            //region menu
            val menu_nav_menu = menu_nav_v.asJsonObject.getAsJsonObject("menu")
            val new_role_nav_menu = JsonObject().apply {
                add("name", menu_nav_menu.get("name"))
                add("title", menu_nav_menu.get("title"))
                add("nav", menu_nav_menu.get("nav"))
                menu_nav_menu.get("roleType")?.let { add("roleType", it) }
                val show_auth_array = JsonArray()
                val show_auth = JsonObject().apply {
                    add("key", Gson().toJsonTree("show"))
                    add("title", Gson().toJsonTree("显示"))
                    add("val", Gson().toJsonTree(true))
                }
                show_auth_array.add(show_auth)
                if (oldRoleObj?.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menu")?.get("auth") == null) {
                    add("auth", show_auth_array)
                } else {
                    show_auth.apply {
                        add("val", oldRoleObj.getAsJsonObject(menu_nav_k).getAsJsonObject("menu").getAsJsonArray("auth")[0].asJsonObject.get("val"))
                    }
                    add("auth", show_auth_array)
                }
            }
            new_role_nav_v.add("menu", new_role_nav_menu)
            //endregion

            //region menuMap
            val menu_nav_menuMap = menu_nav_v.asJsonObject.getAsJsonObject("menuMap")
            val new_role_nav_menuMap = JsonObject()
            menu_nav_menuMap.entrySet().forEach { (menu_nav_menuMap_content_k, menu_nav_menuMap_content_v) ->
                val new_role_nav_menuMap_content = JsonObject().apply {
                    add("id", menu_nav_menuMap_content_v.asJsonObject.get("id"))
                    add("title", menu_nav_menuMap_content_v.asJsonObject.get("title"))
                    add("hide", menu_nav_menuMap_content_v.asJsonObject.get("hide"))
                    menu_nav_menuMap_content_v.asJsonObject.get("roleType")?.let { add("roleType", it) }
                    menu_nav_menuMap_content_v.asJsonObject.get("line")?.let { add("line", it) }
                    menu_nav_menuMap_content_v.asJsonObject.get("style")?.let { add("style", it) }
                    menu_nav_menuMap_content_v.asJsonObject.get("icon")?.let { add("icon", it) }

                    //region 对menuMap层的每个二级菜单的ctrl的处理
                    if (menu_nav_menuMap_content_v.asJsonObject.get("ctrl") == null) {
                        //region show auth 的处理
                        val show_auth_array = JsonArray()
                        val show_auth = JsonObject().apply {
                            add("key", Gson().toJsonTree("show"))
                            add("title", Gson().toJsonTree("显示"))
                            add("val", Gson().toJsonTree(true))
                        }
                        val old_show_auth = oldRoleObj?.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menuMap")?.getAsJsonObject(menu_nav_menuMap_content_k)?.getAsJsonArray("auth")?.find { it -> it.asJsonObject.get("key").asString == "show" }
                        if (old_show_auth == null) {
                            show_auth_array.add(show_auth)
                            add("auth", show_auth_array)
                        } else {
                            show_auth_array.add(old_show_auth)
                        }
                        add("auth", show_auth_array)
                        //endregion
                    }
                    /*ctrl*/
                    else {
                        menu_nav_menuMap_content_v.asJsonObject.get("ctrl")?.let {
                            val new_role_menuMap_authArray = JsonArray()
//                        val menu_menuMap_right_array = JsonParser().parse(sysControllerListService?.getRightCache(it.asJsonObject.get("name").asString).toString()).asJsonObject.getAsJsonArray("items")
                            //TODO("delete")
                            val menu_menuMap_right_array = JsonParser().parse(json).asJsonObject.get("items").asJsonArray
                            val old_role_menuMap_auth = oldRoleObj.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menuMap")?.getAsJsonObject(menu_nav_menuMap_content_k)?.getAsJsonArray("auth")
                            menu_menuMap_right_array.forEach { menu_ctrl_item ->
                                val oldAuth = old_role_menuMap_auth?.find { it -> (it as JsonObject).get("key") == menu_ctrl_item.asJsonObject.get("name") }
                                if (oldAuth == null) {
                                    new_role_menuMap_authArray.add(JsonObject().apply {
                                        add("key", menu_ctrl_item.asJsonObject.get("name"))
                                        add("title", menu_ctrl_item.asJsonObject.get("viewName"))
                                        add("val", Gson().toJsonTree(false))
                                    })
                                } else {
                                    new_role_menuMap_authArray.add(JsonObject().apply {
                                        add("key", menu_ctrl_item.asJsonObject.get("name"))
                                        add("title", menu_ctrl_item.asJsonObject.get("viewName"))
                                        add("val", oldAuth.asJsonObject.get("val"))
                                    })
                                }
                            }
                            //region show auth的处理
                            val show_auth_array = new_role_menuMap_authArray
                            val show_auth = JsonObject().apply {
                                add("key", Gson().toJsonTree("show"))
                                add("title", Gson().toJsonTree("显示"))
                                add("val", Gson().toJsonTree(true))
                            }
                            val old_show_auth = oldRoleObj?.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menuMap")?.getAsJsonObject(menu_nav_menuMap_content_k)?.getAsJsonArray("auth")?.find { it -> it.asJsonObject.get("key").asString == "show" }
                            if (old_show_auth == null) {
                                show_auth_array.add(show_auth)
                                add("auth", show_auth_array)
                            } else {
                                show_auth_array.add(old_show_auth)
                            }
                            //endregion

                            if (new_role_menuMap_authArray.count() > 0) {
                                add("auth", new_role_menuMap_authArray)
                            }
                        }
                    }
                    //endregion

                    //region 处理第三层菜单-items
                    menu_nav_menuMap_content_v.asJsonObject.get("items")?.let {
                        val new_role_itemArray = JsonArray()
                        it.asJsonArray.forEach { item ->
                            new_role_itemArray.add(JsonObject().apply {
                                add("id", item.asJsonObject.get("id"))
                                add("title", item.asJsonObject.get("title"))
                                add("hide", item.asJsonObject.get("hide"))
                                item.asJsonObject.get("roleType")?.let { add("roleType", it) }
                                item.asJsonObject.get("line")?.let { add("line", it) }
                                item.asJsonObject.get("style")?.let { add("style", it) }
                                item.asJsonObject.get("icon")?.let { add("icon", it) }


                                //region 对三级菜单items的auth的处理
                                if (item.asJsonObject.getAsJsonObject("ctrl") == null) {

                                    //region auth show
                                    val show_auth_array = JsonArray()
                                    val show_auth = JsonObject().apply {
                                        add("key", Gson().toJsonTree("show"))
                                        add("title", Gson().toJsonTree("显示"))
                                        add("val", Gson().toJsonTree(true))
                                    }
                                    show_auth_array.add(show_auth)

                                    val current_role_item = oldRoleObj?.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menuMap")?.getAsJsonObject(menu_nav_menuMap_content_k)?.getAsJsonArray("items")?.find { _item -> _item.asJsonObject.get("id") == item.asJsonObject.get("id") }

                                    if (current_role_item == null) {
                                        add("auth", show_auth_array)
                                    } else {
                                        show_auth.apply {
                                            add("val", current_role_item.asJsonObject.getAsJsonArray("auth").find { _it -> _it.asJsonObject.get("key").asString == "show" }?.asJsonObject?.get("val"))
                                        }
                                        add("auth", show_auth_array)
                                    }
                                    //endregion
                                } else {

                                    item.asJsonObject.getAsJsonObject("ctrl")?.let {
                                        /*新的角色的三级菜单的authArray*/
                                        val new_role_item_auth_array = JsonArray()
                                        /*原来该角色的items对应的Array*/
                                        val old_role_items_array = oldRoleObj?.asJsonObject?.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menuMap")?.getAsJsonObject(menu_nav_menuMap_content_k)?.getAsJsonArray("items")

                                        /*该item对应的Controller的method列表*/
//                                        val menu_item_right_array = JsonParser().parse(sysControllerListService?.getRightCache(it.get("name").asString).toString()).asJsonObject.getAsJsonArray("items")

                                        //TODO("delete")
                                        val menu_item_right_array = JsonParser().parse(json).asJsonObject.get("items").asJsonArray
                                        /*遍历菜单的权限列表*/
                                        menu_item_right_array.forEach { menu_right ->
                                            /*判断当前item的id是否在之前的role菜单中已经存在*/
                                            val _old_role_item = old_role_items_array?.find { __it -> __it.asJsonObject.get("id") == item.asJsonObject.get("id") }
                                            if (_old_role_item == null) {
                                                new_role_item_auth_array.add(JsonObject().apply {
                                                    add("key", menu_right.asJsonObject.get("name"))
                                                    add("title", menu_right.asJsonObject.get("viewName"))
                                                    add("val", Gson().toJsonTree(false))
                                                })
                                            } else {
                                                /*匹配role原来的权限信息*/
                                                if (_old_role_item.asJsonObject.get("key") == menu_right.asJsonObject.get("name")) {
                                                    new_role_item_auth_array.add(JsonObject().apply {
                                                        add("key", menu_right.asJsonObject.get("name"))
                                                        add("title", menu_right.asJsonObject.get("viewName"))
                                                        add("val", _old_role_item.asJsonObject.get("val"))
                                                    })
                                                } else {
                                                    new_role_item_auth_array.add(JsonObject().apply {
                                                        add("key", menu_right.asJsonObject.get("name"))
                                                        add("title", menu_right.asJsonObject.get("viewName"))
                                                        add("val", Gson().toJsonTree(false))
                                                    })
                                                }
                                            }
                                        }

                                        //region auth show
                                        val show_auth_array = new_role_item_auth_array
                                        val show_auth = JsonObject().apply {
                                            add("key", Gson().toJsonTree("show"))
                                            add("title", Gson().toJsonTree("显示"))
                                            add("val", Gson().toJsonTree(true))
                                        }
                                        show_auth_array.add(show_auth)

                                        val current_role_item = oldRoleObj?.getAsJsonObject(menu_nav_k)?.getAsJsonObject("menuMap")?.getAsJsonObject(menu_nav_menuMap_content_k)?.getAsJsonArray("items")?.find { _item -> _item.asJsonObject.get("id") == item.asJsonObject.get("id") }

                                        if (current_role_item == null) {
                                            add("auth", show_auth_array)
                                        } else {
                                            show_auth.apply {
                                                add("val", current_role_item.asJsonObject.getAsJsonArray("auth").find { _it -> _it.asJsonObject.get("key").asString == "show" }?.asJsonObject?.get("val"))
                                            }
                                            add("auth", show_auth_array)
                                        }
                                        //endregion


                                        if (new_role_item_auth_array.size() > 0) {
                                            add("auth", new_role_item_auth_array)
                                        }
                                    }
                                }
                                //endregion
                            })
                        }
                        if (new_role_itemArray.count() > 0) {
                            add("items", new_role_itemArray)
                        }
                    }
                    //endregion
                }
                new_role_nav_menuMap.add(menu_nav_menuMap_content_k, new_role_nav_menuMap_content)
            }
            new_role_nav_v.add("menuMap", new_role_nav_menuMap)
            //endregion
        }

        FileUtils.writeStringToFile(File("C:\\Users\\11851\\Desktop\\newRole.json"), Gson().toJson(newRoleObj), "utf-8")
    }


    val json = "{\n" +
            "      \"viewName\": \"\",\n" +
            "      \"name\": \"com.wise.wises.controller.client.ClientActivityController\",\n" +
            "      \"items\": [\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:getData\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:time\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:consume\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:postData\"\n" +
            "        },\n" +
            "        {\n" +
            "          \"viewName\": \"viewName001\",\n" +
            "          \"name\": \"com.wise.wises.controller.client.ClientActivityController:getActivity\"\n" +
            "        }\n" +
            "      ]\n" +
            "    }"
}